package com.cloud.controller;

import com.alibaba.fastjson.JSONObject;
import com.cloud.entity.AlgorithmNotifyEntitiy;
import com.cloud.entity.ConvertTaskEntity;
import com.cloud.entity.ResultEntity;
import com.cloud.service.ConvertTaskService;
import com.cloud.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.logging.Logger;

/**
 * @author Henry.Song
 * @version 2020/11/24
 * @description
 */
@RestController
@RequestMapping("/converttask")
public class ConvertTaskController {

    private static final Logger LOG = Logger.getLogger(ConvertTaskController.class.getName());

    @Autowired
    private ConvertTaskService convertTaskService;

    /**
     * 查询转换任务
     */
    //@RequiresPermissions("converttask:list")
    @GetMapping("/list")
    public ResultEntity listConvertTask(HttpServletRequest request) {
        return convertTaskService.listConvertTask(CommonUtil.request2Json(request));
    }

    //@RequiresPermissions("converttask:add")
    @PostMapping("/add")
    public ResultEntity addConvertTask(@RequestBody ConvertTaskEntity taskEntity) {
        CommonUtil.hasAllRequired(taskEntity, "taskName,taskType,algorithmType,taskFiles");
        //convertCoordPoints(taskEntity);
        return convertTaskService.addConvertTask(taskEntity);
    }

    //@RequiresPermissions("converttask:update")
    @PostMapping("/update")
    public ResultEntity updateConvertTask(@RequestBody ConvertTaskEntity taskEntity) {
        CommonUtil.hasAllRequired(taskEntity, "id");
        //convertCoordPoints(taskEntity);
        return convertTaskService.updateConvertTask(taskEntity);
    }

    //@RequiresPermissions("converttask:update")
    @PostMapping("/new")
    public ResultEntity newConvertTask(@RequestBody ConvertTaskEntity taskEntity) {
        CommonUtil.hasAllRequired(taskEntity, "taskName,taskType,algorithmType,taskFiles");
        //convertCoordPoints(taskEntity);
        return convertTaskService.newConvertTask(taskEntity);
    }

    //@RequiresPermissions("converttask:update")
    @PostMapping("/notify")
    public ResultEntity taskNotify(@RequestBody AlgorithmNotifyEntitiy taskEntity) {
        CommonUtil.hasAllRequired(taskEntity, "taskStatus,id");
        return convertTaskService.taskNotify(taskEntity);
    }

    //@RequiresPermissions("converttask:update")
    @PostMapping("/delete")
    public ResultEntity deleteConvertTask(@RequestBody ConvertTaskEntity taskEntity) {
        CommonUtil.hasAllRequired(taskEntity, "id");
        return convertTaskService.removeConvertTask(taskEntity);
    }

    /**
     * 根据任务文件查询转换任务
     */
    @GetMapping("/queryByTaskFile")
    public ResultEntity getConvertTaskByTaskFile(HttpServletRequest request) {
        JSONObject requestJson = CommonUtil.request2Json(request);
        return convertTaskService.getConvertTaskByTaskFile(requestJson);
    }

    /**
     * 条件查询
     * @param request
     * @return
     */
    @GetMapping("/query")
    public ResultEntity QueryUploadFile(HttpServletRequest request) {
        JSONObject requestJson = CommonUtil.request2Json(request);
        return convertTaskService.getConvertTaskByCondition(requestJson);
    }
}
